package com.wcy.controller.user;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcy.annotation.aop.PreAuth;
import com.wcy.entity.LearningResource;
import com.wcy.entity.UserCollection;
import com.wcy.entity.UserComment;
import com.wcy.entity.pageVo.ApiDescriptor;
import com.wcy.entity.pageVo.ColumnConstructor;
import com.wcy.entity.pageVo.PageData;
import com.wcy.feign.resource.FeignLearningResourceService;
import com.wcy.feign.user.FeignUserCommentService;
import com.wcy.util.JwtTokenUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
public class UserCommentController {
    @Resource
    private FeignUserCommentService feignUserCommentService;
    @Resource
    private FeignLearningResourceService feignLearningResourceService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;
    @GetMapping("userComment/page")
    public ResponseEntity<PageData<UserComment>> getPage(@RequestParam(defaultValue = "1") int pageNum,
                                                         @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(new PageData<>(UserComment.class, Objects.requireNonNull(feignUserCommentService.getPage(pageNum, pageSize).getBody()),  new ApiDescriptor("userComment/add", "userComment/update", "userComment/delete"), ColumnConstructor.create()));
    }
    @GetMapping("userComment/list")
    public ResponseEntity<List<UserComment>> getList() {
        return ResponseEntity.ok(feignUserCommentService.getList().getBody());
    }
    @PostMapping("userComment/add")
    public ResponseEntity<Void> add(@RequestBody UserComment userComment) {
        return feignUserCommentService.add(userComment);
    }
    @PutMapping("userComment/update")
    public ResponseEntity<Void> update(@RequestBody UserComment userComment) {
        return feignUserCommentService.update(userComment);
    }
    @DeleteMapping("userComment/delete/{id}")
    public ResponseEntity<Void> delete(@PathVariable Integer id) {
        return feignUserCommentService.delete(id);
    }

    @GetMapping("userComment/resource/{id}")
    public ResponseEntity<Page<UserComment>> getByResourceIdPage(@RequestParam(name = "pageNum",defaultValue = "1") int pageNum,
                                                                 @RequestParam(name = "pageSize",defaultValue = "10") int pageSize,
                                                                 @PathVariable("id") Integer id){
        return feignUserCommentService.getByResourceIdPage(id,pageNum, pageSize);
    }

    @GetMapping("userComment/user")
    public ResponseEntity<PageData<UserComment>> getBySelf(@RequestParam(name = "pageNum",defaultValue = "1") int pageNum,
                                                       @RequestParam(name = "pageSize",defaultValue = "10") int pageSize){
        return ResponseEntity.ok(new PageData<>(UserComment.class, Objects.requireNonNull(feignUserCommentService.getBySelf(pageNum, pageSize).getBody()),  new ApiDescriptor("", "", "userComment/delete"), ColumnConstructor.create()));
    }

    @PostMapping("userComment/sendComment")
    public ResponseEntity<Void> sendComment(@RequestBody UserComment userComment){
        return feignUserCommentService.sendComment(userComment);
    }

    @DeleteMapping("userComment/user/delete/{id}")
    public ResponseEntity<Void> deleteByUser(@PathVariable("id") Integer id){
        return feignUserCommentService.deleteByUser(id);
    }

    @GetMapping("userComment/author/page")
    public ResponseEntity<PageData<UserComment>> getAuthorPage(@RequestParam(defaultValue = "1") int pageNum,
                                                         @RequestParam(defaultValue = "10") int pageSize,
                                                               HttpServletRequest request) {
        try {
            List<LearningResource> resources = feignLearningResourceService.getByAuthor(jwtTokenUtil.getUserIdFromToken(jwtTokenUtil.getToken(request))).getBody();
            assert resources != null&& !resources.isEmpty();
            List<Integer> resourceIds = resources.stream().map(LearningResource::getResourceId).collect(Collectors.toList());
            return ResponseEntity.ok(new PageData<>(UserComment.class, Objects.requireNonNull(feignUserCommentService.getByResourceIdsPage(pageNum, pageSize, resourceIds).getBody()), new ApiDescriptor("", "", ""), ColumnConstructor.create()));
        }  catch (Exception e) {
            //出现异常或任何事情，默认为无评论
            Page<UserComment> p = new Page<>();
            p.setRecords(new ArrayList<>());
            return ResponseEntity.ok(new PageData<>(UserComment.class, p, new ApiDescriptor("", "", ""), ColumnConstructor.create()));
        }
    }
}
